<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属站点" prop="stationId">
        <el-select v-model="queryParams.stationId" placeholder="请选择站点" clearable filterable>
          <el-option
            v-for="item in stationOptions"
            :key="item.stationId"
            :label="item.stationName + ',' + item.stationIdC"
            :value="item.stationId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生产厂家" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择设备类型" clearable filterable>
          <el-option
            v-for="item in factoryOptions"
            :key="item.factoryId"
            :label="item.factoryName"
            :value="item.factoryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipName">
        <el-input
          v-model="queryParams.equipName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器编号" prop="instrumentNo">
        <el-input
          v-model="queryParams.instrumentNo"
          placeholder="请输入仪器编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipTypeId">
        <el-select v-model="queryParams.equipTypeId" placeholder="请选择设备类型" clearable filterable>
          <el-option
            v-for="item in equipTypeOptions"
            :key="item.equipTypeId"
            :label="item.equipTypeName"
            :value="item.equipTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备型号" prop="equipModel">
        <el-input
          v-model="queryParams.equipModel"
          placeholder="请输入设备型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="equipState">
        <el-select v-model="queryParams.equipState" placeholder="请选择设备状态" clearable>
          <el-option
            v-for="item in equipStateOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['modules:stationEquip:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['modules:stationEquip:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['modules:stationEquip:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['modules:stationEquip:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stationEquipList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点设备ID" align="center" prop="equipId" v-if="false"/>
      <el-table-column label="所属站点" align="center" prop="stationId">
        <template slot-scope="scope">
          <span>{{ stationName(scope.row.stationId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="equipName" />
      <el-table-column label="仪器编号" align="center" prop="instrumentNo" />
      <el-table-column label="设备类型" align="center" prop="equipTypeId" >
        <template slot-scope="scope">
          <span>{{ equipTypeName(scope.row.equipTypeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="equipModel" />
      <el-table-column label="生产厂家" align="center" prop="factoryId" >
        <template slot-scope="scope">
          <span>{{ factoryName(scope.row.factoryId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用开始日期" align="center" prop="useStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" align="center" prop="expireDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="manuDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manuDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="寿命" align="center" prop="life" />
      <el-table-column label="设备状态" align="center" prop="equipState" >
        <template slot-scope="scope">
          {{ equipStateName(scope.row.equipState) }}
        </template>
      </el-table-column>
      <el-table-column label="电压波动区间" align="center" prop="vFluctRange" />
      <el-table-column label="电量波动区间" align="center" prop="eleFluctRange" />
      <el-table-column label="下次保养时间" align="center" prop="nextServiceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextServiceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保养时间间隔" align="center" prop="serviceDateInterval" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['modules:stationEquip:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['modules:stationEquip:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-col :span="12">
          <el-form-item label="所属站点" prop="stationId">
            <el-select v-model="form.stationId" placeholder="请选择站点" clearable filterable>
              <el-option
                v-for="item in stationOptions"
                :key="item.stationId"
                :label="item.stationName + ',' + item.stationIdC"
                :value="item.stationId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="equipName">
            <el-input v-model="form.equipName" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仪器编号" prop="instrumentNo">
            <el-input v-model="form.instrumentNo" placeholder="请输入仪器编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="equipTypeId">
            <el-select v-model="form.equipTypeId" placeholder="请选择设备类型" clearable filterable>
              <el-option
                v-for="item in equipTypeOptions"
                :key="item.equipTypeId"
                :label="item.equipTypeName"
                :value="item.equipTypeId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备型号" prop="equipModel">
            <el-input v-model="form.equipModel" placeholder="请输入设备型号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="factoryId">
            <el-select v-model="form.factoryId" placeholder="请选择生产厂家" clearable filterable>
              <el-option
                v-for="item in factoryOptions"
                :key="item.factoryId"
                :label="item.factoryName"
                :value="item.factoryId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用开始日期" prop="useStartDate">
            <el-date-picker clearable size="small"
                            v-model="form.useStartDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择使用开始日期"
                            style="width: 180px">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="到期日期" prop="expireDate">
            <el-date-picker clearable size="small"
                            v-model="form.expireDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择到期日期"
                            style="width: 180px">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产日期" prop="manuDate">
            <el-date-picker clearable size="small"
                            v-model="form.manuDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择生产日期"
                            style="width: 180px">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="寿命" prop="life">
            <el-input v-model="form.life" placeholder="请输入寿命" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备状态" prop="equipState">
            <el-select v-model="form.equipState" placeholder="请选择设备状态" clearable>
              <el-option
                v-for="item in equipStateOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电压波动区间" prop="vFluctRange">
            <el-input v-model="form.vFluctRange" placeholder="请输入电压波动区间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电量波动区间" prop="eleFluctRange">
            <el-input v-model="form.eleFluctRange" placeholder="请输入电量波动区间" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下次保养时间" prop="nextServiceDate">
            <el-date-picker clearable size="small"
                            v-model="form.nextServiceDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择下次保养时间"
                            style="width: 180px">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="保养时间间隔" prop="serviceDateInterval">
            <el-input v-model="form.serviceDateInterval" placeholder="请输入保养时间间隔" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStationEquip, getStationEquip, delStationEquip, addStationEquip, updateStationEquip, exportStationEquip } from "@/api/modules/stationEquip";
import { listInfoByDto } from "@/api/modules/info";
import { listEquipType } from  "@/api/modules/equipType";
import { listFactory } from "@/api/modules/factory";

export default {
  name: "StationEquip",
  components: {
  },
  data() {
    return {
      // 设备类型选择列表
      equipTypeOptions:[],
      // 所有站点
      allStationList:[],
      // 站点选择列表
      stationOptions:[],
      // 生产厂家选择
      factoryOptions:[],
      // 设备状态
      equipStateOptions:[
        {"key":1,value:'正常'},
        {"key":2,value:'已故障'},
        {"key":3,value:'维修中'}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点设备表格数据
      stationEquipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipName: undefined,
        instrumentNo: undefined,
        equipTypeId: undefined,
        equipModel: undefined,
        equipState: undefined,
        factoryId: undefined,
        stationId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.init();
  },
  methods: {
    /** 初始化 */
    init(){
      let selectDto = {
        stationState:1
      };
      let equipType = {
        equipTypeState:0
      }
      listInfoByDto(selectDto).then(res=>{
        this.stationOptions = res.data;
      });
      listEquipType(equipType).then(res=>{
        this.equipTypeOptions = res.rows;
      });
      listFactory().then(res=>{
        this.factoryOptions = res.rows;
      })
    },

    /** 回显厂家名称 */
    factoryName(factoryId){
      let value = '';
      this.factoryOptions.forEach(factory =>{
        if(factory.factoryId === factoryId){
          value = factory.factoryName;
        }
      });
      return value;
    },

    /** 回显设备类型名称 */
    equipTypeName(equipTypeId){
      let value = '';
      this.equipTypeOptions.forEach(equipType =>{
        if(equipType.equipTypeId === equipTypeId){
          value = equipType.equipTypeName;
        }
      });
      return value;
    },

    /** 回显设备状态 */
    equipStateName(key){
      let value = '';
      this.equipStateOptions.forEach(equip =>{
        if(equip.key === key){
          value = equip.value;
        }
      });
      return value;
    },

    /** 回显站点名称 */
    stationName(stationId){
      let value = '';
      this.stationOptions.forEach(station =>{
        if(station.stationId === stationId){
          value = station.stationName + "," + station.stationIdC;
          return value;
        }
      });
      return value;
    },

    /** 查询站点设备列表 */
    getList() {
      this.loading = true;
      listStationEquip(this.queryParams).then(response => {
        this.stationEquipList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        equipId: undefined,
        stationId: undefined,
        equipName: undefined,
        instrumentNo: undefined,
        equipTypeId: undefined,
        equipModel: undefined,
        factoryId: undefined,
        useStartDate: undefined,
        expireDate: undefined,
        manuDate: undefined,
        life: undefined,
        equipState: undefined,
        vFluctRange: undefined,
        eleFluctRange: undefined,
        nextServiceDate: undefined,
        serviceDateInterval: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.equipId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const equipId = row.equipId || this.ids
      getStationEquip(equipId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站点设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.equipId != null) {
            updateStationEquip(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStationEquip(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const equipIds = row.equipId || this.ids;
      this.$confirm('是否确认删除站点设备编号为"' + equipIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delStationEquip(equipIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有站点设备数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportStationEquip(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
